<template>
  <div id="lotteryActivity">
    <h1 class="con-right-title">活动列表</h1>
    <topSearch :form-inline="formInline" @toSearch="toSearch"></topSearch>
    <div class="table-conBtn">
      <el-button type="warning" icon="el-icon-plus" @click="addActives('add')">
        新增活动
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
    </div>

    <!-- 表格部分 -->
    <el-table :data="tableData" stripe max-height="490" ref="table">
      <el-table-column type="index" label="序号" width="50" align="center">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="activityName"
        label="活动名称"
        min-width="50"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="channels"
        label="展翅一级入量渠道"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <div
            v-for="item in scope.row.channels"
            :key="item.id + 'd'"
            v-show="scope.row.channels"
          >
            {{ item.name }}
          </div>
          <div v-show="scope.row.channels.length === 0">
            暂无一级渠道
          </div>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="inviteMatchingChannel.oneLevelChannelName"
        label="展翅一级入量渠道"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="channels.secondChannel.name"
        label="展翅二级入量渠道"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <div
            v-for="channel in scope.row.channels"
            :key="channel.id + 'z'"
            v-show="scope.row.channels"
          >
            <div v-for="one in channel.secondChannel" :key="one.id + 'y'">
              {{ one.name }}
            </div>
          </div>
          <div v-show="scope.row.channels.length === 0">
            暂无二级渠道
          </div>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="inviteMatchingChannel.twoLevelChannelName"
        label="展翅二级入量渠道"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="状态"
        prop="activityStatus"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.activityStatus == 'NOT_STARTED'">未开始</span>
          <span v-if="scope.row.activityStatus == 'ONGOING'">进行中</span>
          <span v-if="scope.row.activityStatus == 'OVER'">已结束</span>
        </template>
      </el-table-column>
      <el-table-column label="活动时间" min-width="100" align="center">
        <template slot-scope="scope">
          <div>
            <span>
              {{ scope.row.activityStartTime }}
            </span>
          </div>
          <div>
            至
          </div>
          <div>
            {{ scope.row.activityEndTime }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="activityPoster"
        label="活动海报"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <el-image
            fit="fit"
            :src="scope.row.activityPoster"
            style="width:60px;height:60px"
            :preview-src-list="[scope.row.activityPoster]"
          >
            <div slot="error" class="image-slot">
              暂无图片
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="operatorUser.trueName"
        label="操作人"
        min-width="50"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="action"
        label="操作"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <div class="action-wrap">
            <el-row type="flex" justify="space-around">
              <el-col
                :span="6"
                v-if="scope.row.activityStatus === 'NOT_STARTED'"
              >
                <span class="action-wrap__btn" @click="startActives(scope.row)">
                  开始
                </span>
              </el-col>
              <el-col :span="6" v-if="scope.row.activityStatus !== 'OVER'">
                <span class="action-wrap__btn" @click="editActives(scope.row)">
                  编辑
                </span>
              </el-col>
              <el-col
                :span="6"
                v-if="scope.row.activityStatus === 'NOT_STARTED'"
              >
                <span
                  class="action-wrap__btn"
                  @click="handleDeleteActive(scope.row)"
                  style="color: red"
                >
                  删除
                </span>
              </el-col>
              <el-col :span="6" v-if="scope.row.activityStatus === 'ONGOING'">
                <span
                  @click="handleEndingActive(scope.row)"
                  class="action-wrap__btn"
                  style="color: red"
                >
                  结束
                </span>
              </el-col>
              <el-col :span="6" v-if="scope.row.activityStatus === 'ONGOING'">
                <span class="action-wrap__btn" @click="oneCopy(scope.row)">
                  推广
                </span>
              </el-col>
            </el-row>
            <el-row type="flex" justify="space-around" style="margin-top:10px">
              <el-col
                :span="12"
                v-if="scope.row.activityStatus === 'NOT_STARTED'"
              >
                <span class="action-wrap__btn" @click="oneCopy(scope.row)">
                  推广
                </span>
              </el-col>
              <el-col :span="12">
                <span class="action-wrap__btn" @click="showAround(scope.row)">
                  一键复制
                </span>
              </el-col>
              <el-col
                :span="12"
                v-if="scope.row.activityStatus !== 'NOT_STARTED'"
              >
                <span class="action-wrap__btn" @click="viewsData(scope.row)">
                  查看数据
                </span>
              </el-col>
            </el-row>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <promotion ref="oneCopy" :formInline="oneCopyData"></promotion>
    <showRulesDialog :formInline="rules"></showRulesDialog>
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
  </div>
</template>
<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
import {
  // activeLists,
  // endingActive,
  // deleteActive,
  // getCommUrl,
  endPoster,
  deletePoster,
  startPoster,
  getPostActiveList
} from '@/api/activePull/adviserList/index'
import { filterImageMixins } from '@/mixins/filterImage'
import { saveScrollMixins } from '@/mixins/saveScroll'
import topSearch from '@component/activePull/posterList/topSearch' // 上部搜索条件
import showRulesDialog from '@component/markingActive/common/showRulesDialog'
import promotion from '@component/activePull/adviserList/promotion' // 上部搜索条件

export default {
  name: 'lotteryActivities',
  msg: '抽奖活动管理',
  mixins: [filterImageMixins, saveScrollMixins],
  components: {
    topSearch,
    promotion,
    showRulesDialog
  },
  data() {
    return {
      activeList: [],
      rules: {
        show: false,
        rulesData: ''
      },
      oneCopyData: {
        activeId: '',
        show: false,
        url: ''
      },
      typeLists: [],
      collegeList: [],
      srcList: [],
      dataList: [],
      tableData: [],
      total: 0,
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      formInline: {
        activityName: '',
        activityStatus: '',
        endTime: '',
        startTime: ''
      }
    }
  },
  mounted() {
    this.searchList()
    this.getColleges()
    this.getActivityTypes()
  },
  methods: {
    startActives(record) {
      this.$confirm('是否开始？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          startPoster(
            record.activityId,
            (res) => {
              this.$message({
                type: 'success',
                message: '开始成功!'
              })
              this.searchList()
            },
            (err) => {}
          )
        })
        .catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // })
        })
    },
    showRules(val) {
      this.rules.rulesData = val.lotteryActivityConfig.ruleDescribe
      this.rules.show = true
    },
    //一键复制
    showAround(val) {
      this.$router.push({
        name: 'wh:addPoster',
        query: { type: 'copy', id: val.activityId }
      })
    },
    // 编辑
    editActives(val) {
      this.$router.push({
        name: 'wh:addPoster',
        query: { type: 'edit', id: val.activityId }
      })
    },
    initUrl(val) {
      return new Promise((resolve, reject) => {
        let obj = {
          configKey: 'marketing_activity_url'
        }
        getCommUrl(
          obj,
          (res) => {
            let websit = ''
            switch (val.activityInfo.specificActivityType) {
              case '转盘抽奖活动': // 转盘
                websit = '/luck-draw/lucky-wheel?activityId=' + val.id
                break
              case '宫格抽奖活动': // 宫格
                websit = '/luck-draw/lucky-grid?activityId=' + val.id
                break
              case '翻牌抽奖活动': // 翻牌
                websit = '/luck-draw/cards-active?activityId=' + val.id
                break
              default:
                break
            }
            this.oneCopyData.url = res + websit
            resolve(this.oneCopyData.url)
          },
          (err) => {
            reject(err)
          }
        )
      })
    },

    async oneCopy(val) {
      this.oneCopyData.show = true
      this.$refs['oneCopy'].formInline.url = val.activityUrl
      this.$refs['oneCopy'].getQRcode(val.activityUrl).then((img) => {
        this.$refs['oneCopy'].getUrl = img
      })
    },
    handleDeleteActive(val) {
      let _this = this
      this.$confirm('是否删除该活动?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePoster(
          val.activityId,
          () => {
            _this.searchList()
          },
          () => {
            _this.searchList()
          },
          (err) => {
            _this.$message({
              type: 'warning',
              message: err.message
            })
          }
        )
      })
    },
    handleEndingActive(val) {
      let _this = this
      this.$confirm('是否结束？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          endPoster(
            val.activityId,
            (res) => {
              _this.$message({
                type: 'success',
                message: '结束成功!'
              })
              _this.searchList()
            },
            (err) => {}
          )
        })
        .catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // })
        })
    },
    async getActivityTypes() {
      let data = await sysNormalDisable(
        'specific_marketing_activity_type',
        'specific_marketing_activity_type'
      )
      this.typeLists = data
    },
    async getColleges() {
      let data = await sysNormalDisable('sys_college', 'sys_college')
      this.collegeList = data
    },
    viewsData(value) {
      this.$router.push({
        name: 'wh:activePull:viewData',
        query: { value }
      })
    },
    // 新增
    addActives() {
      this.$router.push({
        name: 'wh:addPoster',
        query: { type: 'add' }
      })
    },
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },
    /* 换页 */
    changePage(page) {
      this.searchParams.pageNum = page
      this.searchList()
    },
    /* 获取列表 */
    searchList() {
      let _this = this
      let params = { ...this.searchParams, ...this.formInline }
      getPostActiveList(
        params,
        (res) => {
          this.tableData = res.rows
          this.total = res.total
        },
        (err) => {}
      )
    },
    refreshList() {
      this.searchParams.pageNum = 1
      this.searchList()
    }
  }
}
</script>
<style lang="less" scoped>
//.action-wrap {
//  display: flex;
//  justify-content: space-around;
//  align-items: center;
//  color: #02a7f0;
//}
.action-wrap__btn {
  cursor: pointer;
  color: #02a7f0;
}
.activity-time__wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
}
#lotteryActivity {
  .data-input .el-input__inner {
    padding-left: 30px !important;
  }
  .img-box {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 60px;
    border: 1px solid #ddd;

    .img-inner-box {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
      text-align: center;

      .el-image {
        width: 100%;
        height: 100%;
      }

      img {
        max-width: 100%;
        max-height: 100%;
        cursor: pointer;
      }
    }

    .num {
      position: absolute;
      display: block;
      bottom: 0;
      right: 0;
      width: 16px;
      height: 16px;
      background: rgba(0, 0, 0, 0.7);
      color: #fff;
      font-size: 12px;
      text-align: center;
      line-height: 16px;
    }
  }
}
</style>
